package com.hoshiicloud.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.stock.entity.WarehouseType;
import com.hoshiicloud.stock.mapper.IWarehouseTypeMapper;
import com.hoshiicloud.stock.service.IWarehouseTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 库存单据类型service接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/18 13:58
 * @mail : maiboyc@163.com
 **/
@Service
public class WarehouseTypeServiceImpl extends ServiceImpl<IWarehouseTypeMapper, WarehouseType> implements IWarehouseTypeService {

    /**
     * 根据id获取
     * @param id ID
     * @return Wvtype
     */
    @Cacheable(cacheNames = "WarehouseType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "仓库管理-库存单据类型管理-根据ID查询库存单据类型信息")
    @Override
    public WarehouseType getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据一组id获取
     * @param ids id组
     * @return List<WarehouseType>
     */
    @Cacheable(cacheNames = "WarehouseType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "仓库管理-库存单据类型管理-根据ID组查询库存单据类型信息")
    @Override
    public List<WarehouseType> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 保存
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WarehouseType", allEntries = true)
    @Logable(authName = "仓库管理-库存单据类型管理-新增库存单据类型信息")
    @Override
    public Long insert(WarehouseType entity) {
        // 创建日期 修改日期 创建人 修改人 删除标识
        entity.setCreatedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");// 需要拿当前的登录人
        entity.setDeleted(0);
        //需要做日期转换
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增库存单据类型信息失败"));
        }
        return entity.getId();
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WarehouseType", allEntries = true)
    @Logable(authName = "仓库管理-库存单据类型管理-修改库存单据类型信息")
    @Override
    public Long update(WarehouseType entity) throws ServiceException {
        // 自动填入修改时间 修改人
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setUpdatedBy("admin1");// 需要拿当前的登录人
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改库存单据类型信息失败"));
        }
        return entity.getId();
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<WarehouseType>
     */
    @Cacheable(cacheNames = "WarehouseType", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex+':'+#entity.pageSize+':'+#entity.code+':'+#entity.name+':'+#entity.flag")
    @Logable(authName = "仓库管理-库存单据类型管理-分页查询库存单据类型信息")
    @Override
    public IPage<WarehouseType> List(WarehouseType entity) {
        QueryWrapper<WarehouseType> queryWrapper = new QueryWrapper();
        IPage<WarehouseType> page = new Page();
        /*
        if (null != entity.getPageIndex()) {
            page.setCurrent(entity.getPageIndex());
        }
        if (null != entity.getPageSize()) {
            page.setSize(entity.getPageSize());
        }
         */
        if (StringUtils.isNotEmpty(entity.getCode())) {
            queryWrapper.eq("code", entity.getCode());
        }
        if (StringUtils.isNotEmpty(entity.getName())) {
            queryWrapper.like("name", entity.getName());
        }
        if (null != entity.getFlag()) {
            queryWrapper.eq("flag", entity.getFlag());
        }
        // 默认查询只查询未逻辑删除的数据
        queryWrapper.eq("deleted", 0);
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WarehouseType", allEntries = true)
    @Logable(authName = "仓库管理-库存单据类型管理-删除库存单据类型信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        WarehouseType entity = this.getById(id);
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除库存单据类型信息失败"));
        } else {
            return true;
        }
    }

}
